package com.huang.community.service.Imp;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.community.filter.SensitiveFilter;
import com.huang.community.mapper.DiscussPostMapper;
import com.huang.community.pojo.DiscussPost;
import com.huang.community.service.DiscussPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;
import java.util.List;
/**
 * 帖子 服务类
 * @author TianTian
 * @date 2022/4/28 22:25
 */
@Service
public class DiscussPostServiceImpl extends ServiceImpl<DiscussPostMapper, DiscussPost> implements DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;
    //根据id查询帖子
    @Override
    public DiscussPost getDiscussPostById(int id) {
       return this.discussPostMapper.selectById(id);
    }

    public HashMap<String, Object> findDiscussPosts(Integer userId, int offset, int limit,int orderMode) {

        IPage<DiscussPost> page = new Page<>(offset,limit);
        if(null!=userId){
            if(orderMode==0){
            page = this.baseMapper.selectPage(page, new QueryWrapper<DiscussPost>()
                    .eq("user_id", userId)
                    .orderByDesc("type")
                    .orderByDesc("create_time")
                    .eq("status",1)
                    .or()
                    .eq("user_id", userId)
                    .eq("status",0)
                    );
            }
            else {
                page = this.baseMapper.selectPage(page, new QueryWrapper<DiscussPost>()
                        .eq("user_id", userId)
                        .orderByDesc("type")
                        .orderByDesc("score")
                        .orderByDesc("create_time")
                        .eq("status",1)
                        .or()
                        .eq("status",0)
                        );
            }
        }
        else {
            if(orderMode==0){
            page =this.baseMapper.selectPage(page,new QueryWrapper<DiscussPost>()
                    .orderByDesc("type")
                    .orderByDesc("create_time")
                    .eq("status",1)
                    .or()
                    .eq("status",0)
                    );
        }else {
                page = this.baseMapper.selectPage(page, new QueryWrapper<DiscussPost>()
                        .orderByDesc("type")
                        .orderByDesc("score")
                        .orderByDesc("create_time")
                        .eq("status",1)
                        .or()
                        .eq("status",0)
                         );

            }
        }
        List<DiscussPost> discussPosts = page.getRecords();
        HashMap<String, Object> hm = new HashMap<>();
        hm.put("discussPosts",discussPosts);
        hm.put("total",(int)page.getTotal());
        return hm;
    }
    //查询帖子条数
    public int findDiscussPostRows(Integer userId) {
        if (userId==null){
            return this.baseMapper.selectCount(null);
        }
        return this.baseMapper.selectCount(new QueryWrapper<DiscussPost>().eq("user_id",userId));
    }
    //新增帖子
    public int addDiscussPost(DiscussPost post) {
        if (post == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        // 过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));
        return discussPostMapper.insertDiscussPost(post);
    }
    @Override
    public int updateScore(int id, double score) {
        return discussPostMapper.updateScore(id, score);
    }
    @Override
    public int updateType(int id, int type) {
        return discussPostMapper.updateType(id, type);
    }
    @Override
    public int updateStatus(int id, int status) {
        return discussPostMapper.updateStatus(id, status);
    }
    //帖子总数
    @Override
    public int discussPostCount(int userId) {
        Integer id = this.baseMapper.selectCount(new QueryWrapper<DiscussPost>().eq("id", userId));
        return id;
    }
}
